package com.example.service.impl;

import com.example.service.FileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import java.io.InputStream;
import java.util.UUID;

@Service("minioFileServiceImpl")
@ConditionalOnProperty(name = "file.storage.type", havingValue = "minio", matchIfMissing = true)
public class MinioFileServiceImpl implements FileService {
    
    private static final Logger logger = LoggerFactory.getLogger(MinioFileServiceImpl.class);
    
    @Value("${minio.endpoint}")
    private String endpoint;
    
    @Value("${minio.bucket}")
    private String bucket;
    
    private final MinioClient minioClient;
    
    public MinioFileServiceImpl(MinioClient minioClient) {
        this.minioClient = minioClient;
    }
    
    @Override
    public String upload(MultipartFile file) {
        if (file == null || file.isEmpty()) {
            return null;
        }
        
        try {
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID() + extension;
            
            // 根据文件类型确定目录
            String contentType = file.getContentType();
            String directory = "files";
            if (contentType != null) {
                if (contentType.startsWith("image/")) {
                    directory = "images";
                } else if (contentType.startsWith("application/pdf")) {
                    directory = "documents";
                }
            }
            
            // 完整对象名称
            String fullObjectName = directory + "/" + objectName;
            
            // 上传文件
            InputStream inputStream = file.getInputStream();
            PutObjectArgs args = PutObjectArgs.builder()
                    .bucket(bucket)
                    .object(fullObjectName)
                    .stream(inputStream, file.getSize(), -1)
                    .contentType(contentType)
                    .build();
            
            minioClient.putObject(args);
            inputStream.close();
            
            // 返回文件URL
            return endpoint + "/" + bucket + "/" + fullObjectName;
        } catch (Exception e) {
            logger.error("文件上传失败", e);
            return null;
        }
    }
    
    @Override
    public boolean deleteFile(String objectName) {
        if (objectName == null || objectName.isEmpty()) {
            return false;
        }
        
        try {
            RemoveObjectArgs args = RemoveObjectArgs.builder()
                    .bucket(bucket)
                    .object(objectName)
                    .build();
            
            minioClient.removeObject(args);
            return true;
        } catch (Exception e) {
            logger.error("文件删除失败", e);
            return false;
        }
    }
} 